<!--
@description:
@author: ldb
@time: 2022-11-24 13:52:46
-->
<template>
  <a-form
    ref="baseForm"
    :model="formState"
    :label-col="{ span: 3 }"
    :wrapper-col="{ span: 24 }"
    autocomplete="off"
    @finish="form.onFinish(beforeSubmit)"
    @finishFailed="form.onFinishFailed"
  >
    <base-form-item v-for="item in formItems" :key="item.name" v-model:value="formState[item.name]" v-bind="item" />
    <a-form-item v-show="type !== 1" :wrapper-col="{ span: 24, offset: 10 }">
      <a-button type="primary" html-type="submit">确认</a-button>
      <a-button style="margin-left: 10px" @click="form.cancel">取消</a-button>
    </a-form-item>
  </a-form>
</template>

<script lang="ts" setup>
import { defineProps, reactive, onMounted, watch } from 'vue'
import { useForm } from '@/hooks/base/FormHooks'
import { formItems } from './forms'
import { batchShowDisabledFields, batchUpdateFields, injectCustomUpdateOptions } from '@/utils/form-item-util'
import { NOTICE_API_PRE_SUFFER } from '@/api/system/notice'
import { Notice, NoticeRange } from '@/entity/system/notice'
import { SearchValue } from '@/components/base/form-item/BaseFormItem.vue'
import { deptSelected } from '@/api/system/dept'
import { commonPage } from '@/api/common/common'
import { USER_API_PRE_SUFFER } from '@/api/system/user'
import { TransferOptions, TransferParams } from '@/components/base/form-item/props'
import { PageModel } from '@/entity/base-model'
import { user } from '@/entity/system/user'
import { Dept } from '@/entity/system/dept'
const props = defineProps<{ id: string; type: number }>()
const formState = reactive<Notice>({} as Notice)
const form = useForm(NOTICE_API_PRE_SUFFER, formState, props, formItems)

onMounted(async () => {
  await form.init(() => {
    if (formState.ranges) {
      formState.ranges = formState.ranges.map((val: any) => val.fkDeptId || val.fkUserId)
    }
  })
})
/**
 * 提交之前
 */
function beforeSubmit() {
  if (formState.ranges && formState.ranges.length > 0) {
    // 公告范围更新
    formState.ranges = formState.ranges.map((val) => {
      //  1=全部 2=个人 3=所有部门 4=指定部门
      return {
        noticeRange: formState.noticeRange,
        fkDeptId: formState.noticeRange === 4 ? val : undefined,
        fkUserId: formState.noticeRange === 2 ? val : undefined,
      } as unknown as NoticeRange
    })
  }
}

watch(formState, async () => {
  batchShowDisabledFields(formItems.value, ['ranges'], !(formState.noticeRange == 1 || formState.noticeRange == 3))
  const param = formItems.value.find((val) => val.name === 'ranges')?.params as TransferParams
  if (param) {
    // 个人
    if (formState.noticeRange == 2) {
      const { data } = await commonPage<PageModel<user>>(USER_API_PRE_SUFFER + '/page', {})
      param.dataSource = data.list.map((val: user) => {
        return {
          key: val.id,
          title: val.name,
          description: val.name,
        } as TransferOptions
      })
    }
    // 指定部门
    if (formState.noticeRange == 4) {
      const { data } = await deptSelected()
      param.dataSource = data.map((val: Dept) => {
        return {
          key: val.id,
          title: val.name,
          description: val.name,
        } as TransferOptions
      })
    }
  }
})
</script>

<style scoped lang="scss"></style>
